﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace ShareCode.Util.Helper
{
    public static class EnumHelper
    {
        private static readonly Dictionary<string, List<EnumExtendModel>> container = new();
        public static List<EnumExtendModel> GetAllItems<T>()
        {
            var enumName = typeof(T);
            //enumName.Name
            var key = enumName.Name;

            if (!container.ContainsKey(key))
            {
                List<EnumExtendModel> list = new List<EnumExtendModel>();

                // get enum fileds
                FieldInfo[] fields = enumName.GetFields();
                foreach (FieldInfo field in fields)
                {
                    if (!field.FieldType.IsEnum)
                    {
                        continue;
                    }
                    // get enum value
                    int value = (int)(enumName.InvokeMember(field.Name, BindingFlags.GetField, null, null, null) ?? 0);
                    string text = field.Name;
                    string description = string.Empty;
                    object[] array = field.GetCustomAttributes(typeof(DescriptionAttribute), false);
                    if (array.Length > 0)
                    {
                        description = ((DescriptionAttribute)array[0]).Description;
                    }
                    else
                    {
                        description = ""; //none description,set empty
                    }
                    //add to list

                    list.Add(new EnumExtendModel
                    {
                        Value = value,
                        Name = text,
                        Desc = description
                    });
                }

                container.Add(key, list);
            }

            return container[key];
        }

        public static EnumExtendModel GetEnumExtendModelByValue<T>(int value)
        {
            var a = GetAllItems<T>();
            var aa = a.Find(t => t.Value == value);
            if (aa != default)
            {
                return aa;
            }
            else
            {
                var obj = new EnumExtendModel
                {
                    Value = 0,
                    Name = string.Empty,
                    Desc = string.Empty
                };

                return obj;
            }
        }

        public static string? Value2Name<T>(int value)
        {
            try
            {
                return Enum.GetName(typeof(T), value);
            }
            catch
            {
                return string.Empty;
            }
        }
        public static int? NameToValue<T>(string name)
        {
            var a = GetAllItems<T>();
            var aa = a.Find(t => t.Name == name);
            if (aa != default)
            {
                return aa.Value;
            }
            else
            {
                return 0;
            }
        }
        public static int? DescriptionToValue<T>(string name)
        {
            var a = GetAllItems<T>();

            var aa = a.Find(t => t.Desc == name);

            if (aa != default)
            {
                return aa.Value;
            }
            else
            {
                return 0;
            }
        }

    }

    public class EnumExtendModel
    {
        public int? Value { get; set; }
        public string? Name { get; set; }
        public string? Desc { get; set; }

        public override string? ToString()
        {
            return Desc;
        }
    }
}
